using History;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using VISUALNOVEL;

public class SaveAndLoadSlot : MonoBehaviour
{
    public GameObject root;
    public RawImage previewImage;
    public TextMeshProUGUI titleText;
    public Button deleteBtn;
    public Button saveBtn;
    public Button loadBtn;

    [HideInInspector]public int fileNumber = 0;
    [HideInInspector]public string filePath = "";
    [HideInInspector]public string filePreviewImgPath = "";

    private UIConfirmationMenu uiChoiceMenu => UIConfirmationMenu.instance;
    
    public void PopulateDetail(SaveAndLoadMenu.MenuFunction function)
    {
        if (File.Exists(filePath))
        {
            VNGameSave file = VNGameSave.Load(filePath);
            PopulateDetailFromFile(function, file);
        }
        else
        {
            PopulateDetailFromFile(function, null);
        }

    }

    private void PopulateDetailFromFile(SaveAndLoadMenu.MenuFunction function,VNGameSave file)
    {
        if(file == null)
        {
            titleText.text = $"{fileNumber}. Empty File";
            deleteBtn.gameObject.SetActive(false);
            loadBtn.gameObject.SetActive(false);
            saveBtn.gameObject.SetActive(function == SaveAndLoadMenu.MenuFunction.save);
            previewImage.texture = SaveAndLoadMenu.Instance.emptyFileImg;
        }
        else
        {
            titleText.text = $"{fileNumber}.{file.timeStamp}";
            deleteBtn.gameObject.SetActive(true);
            loadBtn.gameObject.SetActive(function == SaveAndLoadMenu.MenuFunction.load);
            saveBtn.gameObject.SetActive(function == SaveAndLoadMenu.MenuFunction.save);

            byte[] data = File.ReadAllBytes(file.screenshotPath);
            Texture2D screenshotPreview = new Texture2D(1, 1);
            ImageConversion.LoadImage(screenshotPreview, data);
            previewImage.texture = screenshotPreview;

        }
    }


    public void Delete()
    {
        uiChoiceMenu.Show(
            "Delete this file?",
            //Choice 1
            new UIConfirmationMenu.ConfirmationButton(
                "Yes", 
                () => {
                uiChoiceMenu.Show(
                    "Are you sure?",
                    //1.1
                    new UIConfirmationMenu.ConfirmationButton("I am sure",OnConfirmDelete),
                    //1.2
                    new UIConfirmationMenu.ConfirmationButton("Never mind",null)
                    );
                },
                autoCloseOnQuit:false),
            //Choice 2
            new UIConfirmationMenu.ConfirmationButton("No", null)
            ) ;
    }

    private void OnConfirmDelete()
    {
        File.Delete(filePath);
        File.Delete(filePreviewImgPath);
        PopulateDetail(SaveAndLoadMenu.Instance.menuFunction);
    }

    public void Load()
    {
        VNGameSave file = VNGameSave.Load(filePath, activateOnLoad: false);
        SaveAndLoadMenu.Instance.Close(closeAllMenus: true);
        if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name 
            == MainMenuManager.MAIN_MENU_SCENE)
        {
            MainMenuManager.instance.LoadGame(file);
        }
        else
        {
            file.Activate();
        }
    }

    public void Save()
    {
        if (HistoryManager.instance.isViewingHistory)
        {
            UIConfirmationMenu.instance.Show(
                "You cannot save while viewing history.",
                new UIConfirmationMenu.ConfirmationButton("Okay", null)
                ) ;
            return;
        }

        var activeSave = VNGameSave.activeFile;

        activeSave.slotNumber = fileNumber;
        activeSave.Save();

        PopulateDetailFromFile(SaveAndLoadMenu.Instance.menuFunction, activeSave);

    }

}
